/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package udpUnitAdapt;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * @author axelsben
 */
public class udpClient {
    private String serverAddress = "localhost";
    private int useSocket = 4774;
    private javax.swing.JTextArea jta = null;
    private boolean logging = false;

    public void setLogging(boolean bool)
    {
        logging = bool;
    }

    public void setTA(javax.swing.JTextArea jtxta)
    {
        jta = jtxta;
    }

    public void setAddress(String address)
    {
        serverAddress = address;
    }

    public void setSocket(int socket)
    {
        useSocket = socket;
    }

    public String getAddress()
    {
        return serverAddress;
    }

    public int getSocket()
    {
        return useSocket;
    }

    public void send(byte[] buf) throws IOException {

            // get a datagram socket
        DatagramSocket socket = new DatagramSocket();
            // send request
        InetAddress address = InetAddress.getByName(serverAddress);

        DatagramPacket packet = new DatagramPacket(buf, buf.length, address, useSocket);
        socket.send(packet);
        if((logging) && (jta != null))
        {
            jta.setSelectionStart(jta.getText().length());
            jta.setSelectionEnd(jta.getText().length()+1);
            jta.replaceSelection("/ut:" + buf.toString());
            if(jta.getText().length() > 25000)
            {
                int rm = jta.getText().length() - 25000;
                jta.setSelectionStart(0);
                jta.setSelectionEnd(rm);
                jta.replaceSelection("");
            }
            jta.setSelectionStart(jta.getText().length());
            jta.setSelectionEnd(jta.getText().length()+1);
        }

        socket.close();
    }
}
